package com.zhangxun.config;


import com.zhangxun.constant.QueueConstant;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MsgRabbitConfig {


    /**
     * 消息延迟队列
     * @return
     */
    @Bean
    public Queue msgLateQueue(){
        Map<String, Object> map = new HashMap();
        // 设置延迟的时间 设置消息死亡后走哪个交换机  走哪个路由key
        map.put("x-message-ttl", 20000);
        map.put("x-dead-letter-exchange", QueueConstant.DEAD_MSG_EXCHANGE);
        map.put("x-dead-letter-routing-key", QueueConstant.DEAD_MSG_ROUTING_KEY);
        Queue queue = new Queue(QueueConstant.LATE_MSG_QUEUE,true,false,false,map);
       return queue;
    }

    /**
     * 死信交换机
     * @return
     */
    @Bean
    public Queue msgDeadQueue(){
        return new Queue(QueueConstant.DEAD_MSG_QUEUE);
    }

    /**
     * 死信交换机
     * @return
     */
    @Bean
    public DirectExchange msgDeadExchange(){
        return new DirectExchange(QueueConstant.DEAD_MSG_EXCHANGE);
    }

    /**
     * 死信队列和死信交换机绑定
     */
    @Bean
    public Binding msgDeadBinding() {
        return BindingBuilder.bind(msgDeadQueue()).to(msgDeadExchange()).with(QueueConstant.DEAD_MSG_ROUTING_KEY);
    }

}
